package com.black.cat.jvm.instructions.references;


import com.black.cat.jvm.instructions.base.ClassInitLogic;
import com.black.cat.jvm.instructions.base.InstructionIndex16;
import com.black.cat.jvm.instructions.base.MethodInvokeLogic;
import com.black.cat.jvm.rtda.Frame;
import com.black.cat.jvm.rtda.heap.constantpool.MethodRef;
import com.black.cat.jvm.rtda.heap.constantpool.RunTimeConstantPool;
import com.black.cat.jvm.rtda.heap.methodarea.JClass;
import com.black.cat.jvm.rtda.heap.methodarea.JMethod;


//调用静态方法的指令
public class INVOKE_STATIC extends InstructionIndex16 {

    @Override
    public void execute(Frame frame) {
        RunTimeConstantPool runTimeConstantPool = frame.method().clazz().constantPool();
        //解析符号引用后得到方法M
        MethodRef methodRef = (MethodRef) runTimeConstantPool.getConstants(this.idx);
        JMethod resolvedMethod = methodRef.resolvedMethod();

        //M必须是静态方法
        if (!resolvedMethod.isStatic()) {
            throw new IncompatibleClassChangeError();
        }

        //TODO  init
//        JClass clazz = resolvedMethod.clazz();
//
//        if (!clazz.initStarted()) {
//            frame.revertNextPC();
//            ClassInitLogic.initClass(frame.thread(), clazz);
//            return;
//        }

        //调用InvokeMethod（）函数执行该方法
        MethodInvokeLogic.invokeMethod(frame, resolvedMethod);
    }
}
